package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.ItemStatusDao;
import mil.af.amc.adpetracker.model.ItemStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.NonUniqueResultException;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 * Please fill in this Javadoc
 *
 * @author Jason Ferguson
 */
@Repository("itemStatusDao")
public class HibernateItemStatusDao extends HibernateGenericDao<ItemStatus> implements ItemStatusDao {

    private static final Log log = LogFactory.getLog(ItemStatus.class);

    public HibernateItemStatusDao() {
        super(ItemStatus.class);
    }

    public ItemStatus getStatusByName(final String statusDescription) {

        Criteria isCriteria = sessionFactory.getCurrentSession().createCriteria(ItemStatus.class);
        isCriteria.add(Restrictions.eq("status", statusDescription.toUpperCase()));

        ItemStatus result = null;
        try {
            result = (ItemStatus) isCriteria.uniqueResult();
        } catch (NonUniqueResultException ex) {
            log.debug("getStatusByName() threw NonUniqueResultException", ex);
            return null;
        } catch (HibernateException ex) {
            log.debug("getStatusByName() threw HibernateException", ex);
        }

        return result;

//        return (ItemStatus) getHibernateTemplate().execute(new HibernateCallback() {
//            public Object doInHibernate(Session session) throws HibernateException, SQLException {
//
//                Criteria crit = session.createCriteria(ItemStatus.class);
//                crit.add(Restrictions.eq("status", statusDescription.toUpperCase()));
//                return crit.uniqueResult();
//
//            }
//        });
    }

}
